//
//  YXVisitorView.swift
//  YXweibo
//
//  Created by AS on 5/11/16.
//  Copyright © 2016 AS. All rights reserved.
//

import UIKit

// 1.定义协议   <NSObject>
@objc protocol YXVisitorViewDelegate:NSObjectProtocol{
    // 2.定义协议方法
    /// 注册按钮被点击后的代理方法
    optional func visitorViewDidClickRegister()
    
    /// 登录按钮被点击后的代理方法
    func visitorViewDidClickLogin()
}
/// 自定义的访客视图
class YXVisitorView: UIView {

    
    /// 3.定义代理属性
    // @property() id<CZVisitorViewDelegate> delegate;
    weak var delegate:YXVisitorViewDelegate?
    
    // MARK: - 公开的方法
    /// 设置访客视图内容
    func setupVisitorInfo(imageName:String,message:String)
    {
        // 将转轮图片替换
        iconView.image = UIImage(named: imageName)
        
         // 设置messageLabel
        messageLable.text = message
        
        // 隐藏小房子
        homeView.hidden = true
        
        // 隐藏小房子
        coverView.hidden = true
        
        
    }
       /// 转轮旋转动画
    func startRotationAnimation()
    {
         // 创建核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
         // 核心动画完成的时候不要移除
        animation.removedOnCompletion = false
        
        // 将核心动画添加到图层上面
        iconView.layer.addAnimation(animation, forKey: nil)
        
        
        
    }
     // override: 构造函数,看父类有没有和这个一样的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 在super.init之前给属性(存储性属性)赋值
        // 调用对象方法不能放到super.init之前,在super.init之前,这个对象还没初始化好(创建),对象没创建好是不能调用对象的方法的
        prepareUI()
    }
    /// 这个构造函数通过xib/sb里面创建才会走这个方法
    /// UIKit的里面的view即可以通过代码来创建,也可以通过xib/sb来创建
    /// required 表示必须实现的构造函数有required不需要加override
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 设置UI
  private func prepareUI()
  {
    
     // 设置背景颜色
    //        self.backgroundColor = UIColor(red: 232 / 255.0, green: 232 / 255.0, blue: 232 / 255.0, alpha: 1)
    self.backgroundColor = UIColor(white: 237/255.0,alpha:1)
     // 将子控件添加进来
    self.addSubview(iconView)
    //添加遮罩
    self.addSubview(coverView)
    
    self.addSubview(homeView)
    self.addSubview(messageLable)
    self.addSubview(registerButton)
    self.addSubview(loginButton)
    
    
    //1.添加约束之前需要去除Autoresizing属性
    iconView.translatesAutoresizingMaskIntoConstraints = false
    coverView.translatesAutoresizingMaskIntoConstraints = false
    homeView.translatesAutoresizingMaskIntoConstraints = false
    messageLable.translatesAutoresizingMaskIntoConstraints = false
    registerButton.translatesAutoresizingMaskIntoConstraints = false
    loginButton.translatesAutoresizingMaskIntoConstraints = false
    
    // 添加约束(最好只添加一次)
    // 创建的约束都往要约束的view的父控件上面添加
    
    // 转轮
    /// CenterX和父控件的CenterX重合
    
    // item: 要添加约束的view
    // attribute: 要添加的View的属性
    // toItem: 要参照的view
    // attribute: 要参照View的属性
    // 将约束添加到view上面去

     self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    /// CenterY和父控件的CenterY重合
    self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
    
    
    // 遮盖
     self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
    self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    // 小房子
    // CenterX转轮CenterX
    
    self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
    // CenterY转轮CenterY
    self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
    
    
    // 消息label
    /// 宽度
    /// 当参照的view为nil时,属性为NSLayoutAttribute.NotAnAttribute
    self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
    // CenterX 和父控件CenterX
    self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    // Top和转轮底部
      self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
   
    
    // 注册按钮
    /// 顶部和label底部
    self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    
     /// 左边和label对齐
     self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
    
     /// 宽度
    self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    // 高度
    self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

    
    // 登录按钮
    /// 顶部和label底部
    self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    
    /// 右边和label对齐
    self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
    
    /// 宽度
    self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    
    // 高度
    self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

    }
    //MARK: - 按钮点击事件
    // 按钮点击事件方法是系统来调用, 当把按钮点击事件private后系统就找不到这个方法了
    // @objc: 让系统可以找到我们的方法
    @objc private func didClickRegister()
    {
        // view不能Modal出控制器来,需要将事件传递给控制器
        // 4.调用代理的方法
        delegate?.visitorViewDidClickRegister?()
    }
    @objc private func didClickLogin()
    {
                // 4.调用代理的方法
        delegate?.visitorViewDidClickLogin()
    }
    
    // MARK: - 懒加载
    
    /// 遮盖视图
    private lazy var coverView:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    
    /// 所有的控件
    /// 转轮
    private lazy var iconView:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    /// 小房子
    private lazy var homeView:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_house"))
    /// 消息文字
    private lazy var messageLable:UILabel =
        {
            let lable = UILabel()
            
            // 设置label
            lable.text = "关注一些人,看看有什么惊喜!"
            lable.textColor = UIColor.blackColor()
            lable.font = UIFont.systemFontOfSize(16)
            lable.textAlignment = NSTextAlignment.Center
            lable.numberOfLines = 0
            lable.sizeToFit()
            
        return lable
    }()
    /// 注册按钮
    private lazy var registerButton:UIButton = {
        let button = UIButton()
        
        // 设置
  button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //添加按钮点击事件
        button.addTarget(self, action: #selector(YXVisitorView.didClickRegister), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    /// 登录按钮
    private lazy var loginButton:UIButton = {
        
        let button = UIButton()
        
        // 设置
    button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        //添加按钮点击事件
        button.addTarget(self, action: #selector(YXVisitorView.didClickLogin), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}
